package com.zb.system.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjEmpService;
import com.zb.utils.TokenUtil;
import com.zb.vo.EmpSelectVo;
import com.zb.vo.EmpVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商家账号人员组织表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Api(tags = "组织人员类接口")
@RestController
@RequestMapping("/system/emp")
public class ZjEmpController {
    @Resource
    private ZjEmpService empService;

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE,  remark = "组织人员 - 新增或修改人员及人员组织")
    @ApiOperation("新增或修改人员及人员组织变更")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid EmpVo empVo) {
        AccounLoginInfo accountLoginInfo = TokenUtil.getAccountLoginInfo(request);
        empService.save(accountLoginInfo.getCustAccId(), empVo);
        return Result.success();
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.SELECT, remark = "组织人员 - 组织人员列表查询")
    @ApiOperation("组织人员列表查询")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody EmpSelectVo empSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        empSelectVo.setCustAccId(custAccId);
        return Result.success("获取组织人员列表成功").data(empService.getList(empSelectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.SELECT, remark = "组织人员 - 根据职员ID查询职员信息")
    @ApiOperation("根据职员ID查询职员信息")
    @GetMapping("/{empId}")
    public Result getInfo(HttpServletRequest request, @PathVariable @ApiParam(value = "职员ID", required = true) Long empId) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("查询职员信息成功").data("row", empService.getInfo(custAccId, empId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.DELETE, remark = "组织人员 - 删除组织人员")
    @ApiOperation("删除组织人员")
    @DeleteMapping("/{organizeId}")
    public Result deleteEmpId(HttpServletRequest request,
                              @PathVariable @ApiParam(value = "组织ID", required = true) Long organizeId,
                              @RequestBody @ApiParam(value = "组织人员ID集合", required = true) List<Long> idList) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        empService.deleteBatchById(custAccId, organizeId, idList);
        return Result.success("删除组织人员成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "组织人员 - 更改组织人员在职状态")
    @ApiOperation("更改组织人员在职状态")
    @PutMapping("/empDepart")
    public Result empDepart(@RequestParam @ApiParam(value = "是否离职：false-在职，true-已离职", required = true, example = "false") boolean depart,
                            @RequestBody @ApiParam(value = "离职在职人员ID集合", required = true) List<Long> idList) {
        empService.updateEmpDepartBatchById(idList, depart);
        return Result.success("组织人员在职状态修改成功！");
    }

    @ApiOperation("根据账号ID&职员ID获取职员信息")
    @GetMapping("/getEmpInfo/{id}/{empId}")
    public Result getEmpInfo(HttpServletRequest request, @PathVariable(name = "id") @ApiParam(value = "账号ID") Long accountId, @PathVariable @ApiParam(value = "人员ID", required = true) Long empId) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        return Result.success("获取职员信息成功").data(PageUtil.row(empService.getEmpInfo(loginInfo, accountId, empId)));
    }

}
